package com.keeson.android_cli.presenter;


import com.keeson.android_cli.api.Api;
import com.keeson.android_cli.base.BasePresenterImpl;
import com.keeson.android_cli.base.BaseRespose;
import com.keeson.android_cli.base.retrofit.BaseObserver;
import com.keeson.android_cli.base.retrofit.RxSchedulers;
import com.keeson.android_cli.entity.UserInfo;
import com.keeson.android_cli.interfaces.contract.MineContract;

/**
 * Created by jiaqiZHou
 * on 2020/12/4
 */
public class MinePresenter extends BasePresenterImpl<MineContract.View> implements MineContract.Presenter {

    public MinePresenter(MineContract.View view) {
        super(view);
    }

    @Override
    public void modifyUserInfo(UserInfo postBody) {
         view.showProgress(null);
        Api.getInstance().modifyUserInfo(postBody)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<String>>() {
                    @Override
                    protected void onSuccess(BaseRespose<String> data) {
                        view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.modifyUserInfoSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        view.dismissProgress();
                        super.onError(msg, errorCode);
                    }
                });
    }

    @Override
    public void getUserInfoById(int id) {
        //view.showProgress(null);
        Api.getInstance().getUserInfoById(id)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<UserInfo>>() {
                    @Override
                    protected void onSuccess(BaseRespose<UserInfo> data) {
                        //view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.getUserInfoSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        //view.dismissProgress();
                        super.onError(msg, errorCode);
                    }
                });
    }

    @Override
    public void getBedMattressInfo(int id) {
        view.showProgress(null);
        Api.getInstance().getBedMattressInfo(id)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<UserInfo>>() {
                    @Override
                    protected void onSuccess(BaseRespose<UserInfo> data) {
                        view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.getBedMattressInfoSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        view.dismissProgress();
                        super.onError(msg, errorCode);
                    }
                });
    }
}